package com.hz.framework.MyAction;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;

import com.hz.framework.dao.CommonDao;
import com.hz.supermanage.bean.Hzsyspersonnel;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

	public class EncodeInterceptor implements Interceptor {
	    

	    public void destroy() {
	        System.out.println("destroy...");
	    }

	    public void init() {
	    	//服务器启动时加载
	        System.out.println("init...");
	    }

	    public String intercept(ActionInvocation invocation) throws Exception {
	        //这里设置所需编码
	    	HttpServletRequest request = ServletActionContext. getRequest(); 
	    	request.setCharacterEncoding("UTF-8");
			if(com.hz.admin.action.LoginAction.class==invocation.getAction().getClass()){
				return invocation.invoke();
			}
			boolean bool = false;
			HttpSession session = ServletActionContext.getRequest().getSession(false);
			if (session != null) {
				Hzsyspersonnel user = (Hzsyspersonnel)session.getAttribute("admin_user");
				if (user != null) {
					bool=true;
					//("==查看当前用户是否有访问权限==");
//					if(user.getRoleId().equals("")){
//						bool=true;
//					}else{
//						String sql="select * from rights ";
//					}
				}
			}
			if (bool) {
				return invocation.invoke();
			} else {
				PrintWriter out=ServletActionContext.getResponse().getWriter();
				out.append("<script>window.parent.parent.location.href='"+request.getContextPath()+"/login.action';</script>");
				out.flush();
				out.close();
				return null;
			}
	    }
	}

